package com.genexususa.eo.bing.data.requests;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.Ad;
import com.genexususa.eo.bing.data.GxItemException;
import com.genexususa.eo.bing.data.wsSubclasses.GxMobileAd;
import com.genexususa.eo.bing.data.wsSubclasses.GxTextAd;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GxAdsAdGroupRequest {
    private long gxAdGroupId;
    private long bingAdGroupId;
    private Ad[] ads;

    public GxAdsAdGroupRequest(Element element) throws IllegalArgumentException, GxItemException {
        if (element.getElementsByTagName("GxAdGroupId").getLength() > 0 &&
            element.getElementsByTagName("GxAdGroupId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxAdGroupId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxAdGroupId = Long.valueOf(((Node)element.getElementsByTagName("GxAdGroupId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external AdGroup id");
        }
        if (element.getElementsByTagName("BingAdGroupId").getLength() > 0 &&
            element.getElementsByTagName("BingAdGroupId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingAdGroupId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.bingAdGroupId = Long.valueOf(((Node) element.getElementsByTagName("BingAdGroupId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing Bing AdGroup id");
        }
        NodeList adsNode = element.getElementsByTagName("AdCollection");
        if (adsNode.getLength() > 0) {
            NodeList mobileAdsNode = ((Element)adsNode.item(0)).getElementsByTagName("MobileAdCollection");
            NodeList textAdsNode = ((Element)adsNode.item(0)).getElementsByTagName("TextAdCollection");
            if (mobileAdsNode.getLength() > 0 || textAdsNode.getLength() > 0) {
                NodeList mobileAdNode = ((Element)adsNode.item(0)).getElementsByTagName("MobileAd");
                NodeList textAdNode = ((Element)adsNode.item(0)).getElementsByTagName("TextAd");
                this.ads = new Ad[mobileAdNode.getLength() + textAdNode.getLength()];
                int temp = 0;
                for (int i=0; i<mobileAdNode.getLength(); i++) {
                    ads[temp] = new GxMobileAd((Element)mobileAdNode.item(i));
                    temp++;
                }
                for (int i=0; i<textAdNode.getLength(); i++) {
                    ads[temp] = new GxTextAd((Element)textAdNode.item(i));
                    temp++;
                }
            } else {
                throw new GxItemException("No Ads were found for AdGroup " + this.gxAdGroupId);
            }
        } else {
            throw new GxItemException("No Ads were found for AdGroup " + this.gxAdGroupId);
        }
    }

    public long getGxAdGroupId() {
        return gxAdGroupId;
    }

    public long getBingAdGroupId() {
        return bingAdGroupId;
    }

    public Ad[] getAds() {
        return ads;
    }
}
